package com.ruoyi.project.im.friends.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.im.friends.domain.Friends;
import com.ruoyi.project.im.friends.service.IFriendsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户好友Controller
 *
 * @author ruoyi
 * @date 2022-10-11
 */
@Controller
@RequestMapping("/IM/friends")
public class FriendsController extends BaseController
{
    private String prefix = "IM/friends";

    @Autowired
    private IFriendsService friendsService;

    @RequiresPermissions("IM:friends:view")
    @GetMapping()
    public String friends()
    {
        return prefix + "/friends";
    }

    /**
     * 查询用户好友列表
     */
    @RequiresPermissions("IM:friends:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Friends friends)
    {
        startPage();
        List<Friends> list = friendsService.selectFriendsList(friends);
        return getDataTable(list);
    }

    /**
     * 导出用户好友列表
     */
    @RequiresPermissions("IM:friends:export")
    @Log(title = "用户好友", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Friends friends)
    {
        List<Friends> list = friendsService.selectFriendsList(friends);
        ExcelUtil<Friends> util = new ExcelUtil<Friends>(Friends.class);
        return util.exportExcel(list, "用户好友数据");
    }

    /**
     * 新增用户好友
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户好友
     */
    @RequiresPermissions("IM:friends:add")
    @Log(title = "用户好友", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Friends friends)
    {
        return toAjax(friendsService.insertFriends(friends));
    }

    /**
     * 修改用户好友
     */
    @RequiresPermissions("IM:friends:edit")
    @GetMapping("/edit/{ownerUserId}")
    public String edit(@PathVariable("ownerUserId") String ownerUserId, ModelMap mmap)
    {
        Friends friends = friendsService.selectFriendsByOwnerUserId(ownerUserId);
        mmap.put("friends", friends);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户好友
     */
    @RequiresPermissions("IM:friends:edit")
    @Log(title = "用户好友", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Friends friends)
    {
        return toAjax(friendsService.updateFriends(friends));
    }

    /**
     * 删除用户好友
     */
    @RequiresPermissions("IM:friends:remove")
    @Log(title = "用户好友", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(friendsService.deleteFriendsByOwnerUserIds(ids));
    }
}
